/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.activity;

import android.content.Intent;
import android.widget.Button;
import android.widget.ListView;

import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.future.core.AgnettyResult;
import com.ouertech.android.agm.lib.base.utils.UtilList;
import com.ouertech.android.agm.lib.ui.base.defaults.activity.BaseTopActivity;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Address;
import com.simon.mengkou.data.cache.DataCacheImpl;
import com.simon.mengkou.future.base.OuerFutureListener;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.ui.adapter.AddressManagerAdapter;
import com.simon.mengkou.utils.UtilOuer;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/3/20.
 * desc    : 地址管理界面
 */
public class AddressManagerActivity extends BaseTopActivity {
    //地址列表最多个数
    private static final int ADDRESS_SIZE = 10;

    //地址列表
    @Bind(R.id.address_manager_id_list)
    ListView mLvList;

    //新建收货地址
    @Bind(R.id.address_manager_id_new)
    Button mBtnNew;

    private AddressManagerAdapter mAdapter;

    @Override
    protected void initTop() {
        setTitle(R.string.address_manager_title);
        setNavigation(R.drawable.common_ic_left_nav);
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_address_manager);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        //注册地址信息修改广播
        registerAction(CstOuer.BROADCAST_ACTION.ADDRESS_CHANGE_ACTION);

        mAdapter = new AddressManagerAdapter(this);
        mLvList.setAdapter(mAdapter);
        //获取缓存
        List<Address> addresses = OuerApplication.mCacheFactory
                .getCache(DataCacheImpl.AddressListCache.class, OuerApplication.mUser.getUserId())
                .getData();

        if(UtilList.isNotEmpty(addresses)) {
            mLvList.setTag(false);
            mAdapter.setList(addresses);
            refreshAddNewAddress();
        } else {
            mLvList.setTag(true);
        }

        setOnRetryListener(new OnRetryListener() {
            @Override
            public void onRetry() {
                getAddressList();
            }
        });

        //从服务端获取收货地址列表
        getAddressList();
    }

    @Override
    protected void onReceive(Intent intent) {
        super.onReceive(intent);
        //地址信息修改广播
        if(CstOuer.BROADCAST_ACTION.ADDRESS_CHANGE_ACTION.equals(intent.getAction())) {
            getAddressList();
        }
    }

    /**
     * 获取地址列表
     */
    private void getAddressList() {
        AgnettyFuture future = OuerApplication.mOuerFuture.getAddressList(
                OuerApplication.mUser.getUserId(),
                new OuerFutureListener(this) {
                    @Override
                    public void onStart(AgnettyResult result) {
                        super.onStart(result);

                        boolean flag = (boolean) mLvList.getTag();
                        if(flag) {
                            setLoading(true);
                        }
                    }

                    @Override
                    public void onComplete(AgnettyResult result) {
                        super.onComplete(result);
                        boolean flag = (boolean) mLvList.getTag();
                        if(flag) {
                            setLoading(false);
                            mLvList.setTag(false);
                        }

                        List<Address> address = (List<Address>) result.getAttach();

                        mAdapter.setList(address);
                        refreshAddNewAddress();
                    }

                    @Override
                    public void onException(AgnettyResult result) {
                        super.onException(result);
                        boolean flag = (boolean) mLvList.getTag();
                        if(flag) {
                            setRetry(true);
                        }
                    }

                    @Override
                    public void onNetUnavaiable(AgnettyResult result) {
                        super.onNetUnavaiable(result);
                        boolean flag = (boolean) mLvList.getTag();
                        if(flag) {
                            setRetry(true);
                        }
                    }
                }
        );

        attachDestroyFutures(future);
    }

    /**
     * 新建收货地址
     */
    @OnClick(R.id.address_manager_id_new)
    public void addNewAddress() {
        OuerDispatcher.presentEditAddressActivity(this, null);
    }

    /**
     * 删除收货地址
     */
    public void deleteAddress(final Address address) {
        if(address == null) {
            return;
        }

        AgnettyFuture future = OuerApplication.mOuerFuture.deleteAddress(address.getId(),
                new OuerFutureListener(this) {
                    @Override
                    public void onStart(AgnettyResult result) {
                        super.onStart(result);
                        setWaitingDialog(true);
                    }

                    @Override
                    public void onComplete(AgnettyResult result) {
                        super.onComplete(result);
                        setWaitingDialog(false);
                        Boolean flag = true;

                        if(flag) {
                            UtilOuer.toast(AddressManagerActivity.this, R.string.address_manager_delete_success);
                            mAdapter.removeItem(address);
                            refreshAddNewAddress();
                        } else {
                            UtilOuer.toast(AddressManagerActivity.this, R.string.address_manager_delete_failure);
                        }
                    }

                    @Override
                    public void onException(AgnettyResult result) {
                        super.onException(result);
                        setWaitingDialog(false);
                        UtilOuer.toast(AddressManagerActivity.this, R.string.address_manager_delete_failure);
                    }

                    @Override
                    public void onNetUnavaiable(AgnettyResult result) {
                        super.onNetUnavaiable(result);
                        setWaitingDialog(false);
                    }
                }
            );

        attachDestroyFutures(future);
    }

    /**
     * 检查添加地址是否可用
     */
    private void refreshAddNewAddress() {
        if(mAdapter.getCount() >= ADDRESS_SIZE) {
            mBtnNew.setEnabled(false);
        } else {
            mBtnNew.setEnabled(true);
        }
    }
}
